package com.shelpe.services.scheduler.item.product;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shelpe.services.scheduler.dto.ItemsDto;
import com.shelpe.services.scheduler.mongorepository.UnrecoverableErrorRepository;
import com.shelpe.services.scheduler.po.SyncLog;
import com.shelpe.services.scheduler.po.Users;
import com.shelpe.services.scheduler.syncclient.ProductClient;
import com.shelpe.services.scheduler.util.Util;

@Component("productProcessor")
public class ProductProcessor implements ItemProcessor<Users, SyncLog>{

	private static final Logger logger = LoggerFactory.getLogger(ProductProcessor.class);
	
	@Autowired
	private ProductClient proClient;
	
	@Autowired
	private UnrecoverableErrorRepository errorRep;
	
	@Override
	public SyncLog process(Users item) throws Exception {
		// TODO Auto-generated method stub		

		long userId = item.getUserId();
		if(errorRep.countByUserId(userId) > 0){
			return null;
		}
		
		ItemsDto dto = proClient.syncProductsByUserId(userId);
		proClient.syncProductCategory(userId);//不记日志
		
		return Util.convert(dto);
	}

}
